package org.farrell.EOSCalc;

import static java.lang.Math.sqrt;
import static java.lang.Math.abs;
import static java.lang.Math.log;

public class SRKEOS extends CubicEOS implements Compound {
	// This implements the Soave-Redlich-Kwong equation of state
	
	public static double kappa;

	public SRKEOS(double criticalTemperature, double criticalPressure,
			double acentricFactor) {
		super(criticalTemperature, criticalPressure, acentricFactor);
		kappa = 0.48508+w*(1.55171-0.71613*w);
	}

	public double getAlpha(double Temperature) {
		double Tr = Temperature/Tc;
		double inter = 1+kappa*(1-sqrt(Tr));
		return inter*inter;
	}
	
	@Override
	public double getA(double Temperature, double Pressure) {
		// Calculates the A constant for the SRK EOS
		double alpha = getAlpha(Temperature);
		double Pr = Pressure/Pc;
		double invTr = Tc/Temperature;
		return 0.42748*alpha*Pr*invTr*invTr;
	}

	@Override
	public double getB(double Temperature, double Pressure) {
		// Calculates the B constant for the SRK EOS
	    double Pr = Pressure/Pc;
	    double invTr = Tc/Temperature;
	    return 0.08664*Pr*invTr;
	}

	@Override
	public Double[] getZ(double a, double b) {
		double a2 = -1.0;
		double a1 = a-b*(1+b);
		double a0 = -a*b;
		return cubicRoots(a0,a1,a2);
	}

	@Override
	public double getHdepart(double z, double a, double b, double temp) {
		double m = (Tc*getAlpha(temp))/temp;
		return z-1-4.9339796860572491*(m+kappa*sqrt(m))*log(abs((z+b)/z));
	}

	@Override
	public double getSdepart(double z, double a, double b, double temp) {
		double m = (Tc*getAlpha(temp))/temp;
		return log(z-b)-4.9339796860572491*kappa*sqrt(m)*log(abs((z+b)/z));
	}

	@Override
	public double getGdepart(double z, double a, double b, double temp) {
		return z-1-log(z-b)-(a/b)*log(abs((z+b)/z));
	}

}
